package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baozun.mapper.TChatMapper;
import com.baozun.model.TChat;
import com.baozun.model.TChatExample;
import com.baozun.service.ChatService;
import com.baozun.util.PageData;

@Service
public class ChatServiceImpl implements ChatService{
    @Autowired
    private TChatMapper ChatMapper;

    @Override
    public void add(TChat bean) {
        if (StringUtils.isEmpty(bean.getStChatId())) {
            bean.setStChatId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        ChatMapper.insert(bean);
    }

    @Override
    public TChat get(String id) {
        return ChatMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(TChat bean) {
        bean.setDtCreateTime(new Date());
        ChatMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public void delete(String sTChatId) {
        ChatMapper.deleteByPrimaryKey(sTChatId);
    }

    @Override
    public PageData<TChat> getPageListByExample(TChatExample record, int pageSize, int currentPage) {
        // TODO Auto-generated method stub
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TChat> infoList = new PageData<TChat>();
        List<TChat> ChatList = ChatMapper.selectByExampleWithBLOBs(record);
        int count = ChatMapper.countByExample(record);
        infoList.addAll(ChatList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }

    @Override
    public List<TChat> getList(TChatExample record) {
        // TODO Auto-generated method stub
        return ChatMapper.selectByExampleWithBLOBs(record);
    }

}
